package com.hq.fxh.No6UerSpace.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Modle.GetMyPostModle;
import com.hq.fxh.Modle.SpaceUserPostModle;
import com.hq.fxh.Modle.UserPlateModel;
import com.hq.fxh.No0_ChengQu.Login.LoginActivity;
import com.hq.fxh.No6UerSpace.Adapter.Radapter_UserSpace_Plate;
import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.SharedPreferenceUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.R.attr.data;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/9/19
 * @Email xlwenhao1858@sina.com
 */


public class Fragment_UserSpace_Plate extends Fragment {
    protected View rootView;
    protected RecyclerView recycleUserSpace;
    private Radapter_UserSpace_Plate adapter;
    private int memberId;
    List<UserPlateModel> postData;

    public void setMemberId(int memberId) {
        this.memberId = memberId;
        getUserPlage(memberId, 1, 10);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_userspace_plate, null);
        initView(rootView);
        initData();
        return rootView;
    }

    private void initData() {


        memberId = SharedPreferenceUtils.getMemberId(getContext());
        if (memberId == 0) {
            startActivity(new Intent(getContext(), LoginActivity.class));
            getActivity().finish();
        }
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recycleUserSpace.setLayoutManager(linearLayoutManager);

        linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);


        adapter = new Radapter_UserSpace_Plate();
        recycleUserSpace.setAdapter(adapter);
    }

    public static Fragment_UserSpace_Plate newInstance() {

        Bundle args = new Bundle();
        Fragment_UserSpace_Plate fragment = new Fragment_UserSpace_Plate();
        fragment.setArguments(args);
        return fragment;
    }

    private void initView(View rootView) {
        recycleUserSpace = (RecyclerView) rootView.findViewById(R.id.recycle_UserSpace);
    }

    public void getUserPlage(int uid, final int page, int pagesize) {
        UserServiceImpl.GetUserPostList(uid, page, pagesize, new Callback<SpaceUserPostModle>() {
            @Override
            public void onResponse(Call<SpaceUserPostModle> call, Response<SpaceUserPostModle> response) {
                try {
                    SpaceUserPostModle body = response.body();
                    if (body != null) {
                        List<UserPlateModel> data = body.getData();
                        if (postData == null) {
                            postData = new ArrayList<UserPlateModel>();
                        }
                        if (page == 1) {
                            postData.clear();
                        }
                        postData.addAll(response.body().getData());

                        adapter.setData(postData);

                        adapter.notifyDataSetChanged();

                    } else {

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(Call<SpaceUserPostModle> call, Throwable t) {

            }
        });
    }


}
